package com.centsuse.my_redis.config;

import com.centsuse.my_redis.config.serializers.CustomJsonJacksonCodec;
import com.centsuse.my_redis.config.serializers.CustomJsonRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
 * @author bobo
 */
@Configuration
@EnableCaching
public class RedisConfig {

    /**
     * 配置RedisTemplate，使用Jackson序列化值
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // CustomJsonRedisSerializer
        CustomJsonRedisSerializer serializer = new CustomJsonRedisSerializer();

        // 设置Key和HashKey为字符串序列化
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());

        // 设置Value和HashValue为JSON序列化
        template.setValueSerializer(serializer);
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }

    /**
     * 配置RedisCacheManager，使用与RedisTemplate相同的序列化方案
     */
    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
        // 配置Jackson序列化器
        CustomJsonRedisSerializer serializer = new CustomJsonRedisSerializer();
        // 配置缓存序列化
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofHours(1))
                .computePrefixWith(cacheName -> cacheName + ":")
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer));

        return RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
    }

    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient() {
        Config config = new Config();
        // 使用单机模式，根据实际情况调整地址
        config.useSingleServer()
                .setAddress("redis://127.0.0.1:6379")
                .setPassword("123456");

        // 关键配置：使用 JsonJacksonCodec 确保序列化方式与 RedisTemplate 一致
        config.setCodec(new CustomJsonJacksonCodec());

        return Redisson.create(config);
    }
}